#include <linux/kernfs.h>
#include <linux/module.h>
#include <linux/init.h>

static struct kernfs_root *root_kn;
static struct kernfs_node *file_kn;

static int __init my_init(void)
{
    // 创建根目录
    root_kn = kernfs_create_root(NULL, 0, NULL);
    if (IS_ERR(root_kn)) {
        printk(KERN_ERR "Failed to create root directory\n");
        return PTR_ERR(root_kn);
    }

		// 创建一个文件
    file_kn = kernfs_create_file(root_kn->kn, "my_file", 0644, 0, NULL, NULL);
    if (IS_ERR(file_kn)) {
        printk(KERN_ERR "Failed to create file\n");
        return PTR_ERR(file_kn);
    }

    printk(KERN_INFO "kernfs root and file created\n");
    return 0;
}

static void __exit my_exit(void)
{
    // 删除文件
    kernfs_remove(root_kn->kn);

    printk(KERN_INFO "kernfs root and file removed\n");
}

module_init(my_init);
module_exit(my_exit);

MODULE_LICENSE("GPL");
MODULE_AUTHOR("Your Name");
MODULE_DESCRIPTION("Example of using kernfs");
